package com.example.blog.web;

import com.example.blog.pojo.blog;
import com.example.blog.pojo.tag;
import com.example.blog.pojo.type;
import com.example.blog.service.tagService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class blogController {

    @Autowired
    private com.example.blog.service.blogService blogService;
    @Autowired
    private com.example.blog.service.typeService typeService;
    @Autowired
    private tagService tagService;
    @Autowired
    private com.example.blog.service.blogAndTagService blogAndTagService;

    /**
     * 查询所有blog以及type
     * @param page
     * @param model
     * @return
     */
    @GetMapping("/admin/blogs")
    public String adminBlogs(@RequestParam(value = "page" ,defaultValue = "1") Integer page, Model model, HttpSession session){
        PageHelper.startPage(1,10);
        List<type> types = typeService.listType();
        model.addAttribute("types",types);
        if (session.getAttribute("page") != null){
            List<blog> list = (List<blog>) session.getAttribute("page");
            session.removeAttribute("page");
            PageInfo<blog> info = new PageInfo<>(list);
            model.addAttribute("page",info);
            model.addAttribute("message","查询成功");
        }else {
            PageInfo<blog> info = new PageInfo<>(blogService.listBlog());
            model.addAttribute("page",info);
        }
        return "admin/blogs";
    }

    @GetMapping("/admin/blogs/input")
    public String adminBlogsInput(Integer id,Model model){
        List<type> types = typeService.listType();
        List<tag> tags = tagService.listTag();
        model.addAttribute("types",types);
        model.addAttribute("tags",tags);
        model.addAttribute("blog",blogService.getBlog(id));
        return "admin/blogs-input";
    }

    /**
     * 根据title查询blog
     * 根据type查询blog
     * 使用模糊查询
     *
     * @param title
     * @param attributes
     * @return
     */
    @PostMapping("/admin/blogs/find")
    public String adminBlogsFind(String title,String type,boolean recommend, RedirectAttributes attributes, HttpSession session){
        if (title.equals("") && type != null){
            List<blog> list = blogService.getBlog("%"+title+"%",type,recommend);
            session.setAttribute("page",list);
        }if (type == null){
            List<blog> list = blogService.getBlog("%"+title+"%",recommend);
            session.setAttribute("page",list);
        }if (type != null){
            List<blog> list = blogService.getBlog("%"+title+"%",type,recommend);
            session.setAttribute("page",list);
        }if (title.equals("")){
            return "redirect:/admin/blogs";
        }
        return "redirect:/admin/blogs";
    }

    /**
     * 删除blog
     *  根据id删除blog以及blog_tag
     * @param id
     * @param attributes
     * @return
     */
    @Transactional
    @GetMapping("/admin/blogs/delete")
    public String adminBlogsDelete(Integer id,RedirectAttributes attributes){
        attributes.addFlashAttribute("message","删除成功");
        blogService.deleteBlog(id);
        blogAndTagService.delete(id);
        return "redirect:/admin/blogs";
    }

    /**
     * 保存blog以及blog_tag
     *  根据获取到的type字符串查找typeid
     *  根据获取到的tags字符串利用“，”分割字符串,保存到string数组，并根据名称查询获取tag，利用获取到的tag获取id
     * 更新blog以及blog_tag
     *  如果有传入id就是编辑
     *
     * @param attributes
     * @return
     */
    @Transactional
    @PostMapping("/admin/blogs/save")
    public String adminBlogsSave(blog blog,String typeId,String tagIds,RedirectAttributes attributes,HttpSession session) throws NotFoundException {
        if (blog.getId() != null){
            blog.setType_id(typeService.getType(typeId).getId());
            blog.setUpdateTime(new Date());
            blogService.updateBlog(blog.getId(),blog);
            blogAndTagService.delete(blog.getId());
            String[] tag = tagIds.split(",");
            for (String t : tag) {
                blogAndTagService.save(blog.getId(),tagService.getTag(t).getId());
            }
            attributes.addFlashAttribute("message","更新成功");
            return "redirect:/admin/blogs";
        }
        blog.setType_id(typeService.getType(typeId).getId());
        blog.setCreatTime(new Date());
        blog.setView(0);
        blogService.saveBlog(blog);
        String[] tag = tagIds.split(",");
        List<blog> blogs = blogService.getBlog(blog.getTitle(),typeId,blog.isRecommened());             //利用title以及type以及recommend获取list《blog》
        blog = blogs.get(blogs.size()-1);                                                               //利用将list里最后一个blog拿出来
        for (String t : tag) {
            blogAndTagService.save(blog.getId(),tagService.getTag(t).getId());
        }
        if (blog.isPublished()){
            attributes.addFlashAttribute("message","发布成功");
        }else {
            attributes.addFlashAttribute("message","保存成功");
        }
        return "redirect:/admin/blogs";
    }



}
